package com.zheng.study.web.controller.main.company;

import com.zheng.study.base.bean.search.result.SearchResultPaging;
import com.zheng.study.company.entity.Department;
import com.zheng.study.company.entity.Duty;
import com.zheng.study.company.entity.Power;
import com.zheng.study.company.entity.User;
import com.zheng.study.company.service.DepartmentService;
import com.zheng.study.company.service.DutyService;
import com.zheng.study.company.service.PowerService;
import com.zheng.study.company.service.UserService;
import com.zheng.study.web.controller.BaseController;
import com.zheng.study.web.shiro.bind.annotation.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 职责信息
 * ============================================================================
 * author : dzw
 * createDate:  2018/11/13 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main/duty")
public class DutyController extends BaseController {
	private static final long serialVersionUID = -229457650714975366L;

	@Autowired
	private UserService userService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private PowerService powerService;
	@Autowired
	private DutyService dutyService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "list_department", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> list_department(@CurrentUser User user, String departmentId, Integer draw){
		Map<String, Object> data = new HashMap<String, Object>();
		Department department = departmentService.get(departmentId);
		if(department!=null){
			Duty leader = department.getLeader();
			SearchResultPaging searchResult = dutyService.getDutyPageByDept(null, null, null, department);
			List<Duty> dutyList = searchResult.getList();
			List<Map<String, Object>> dataRows = new ArrayList<Map<String, Object>>();
			Map<String, Object> rMap;
			for(Duty duty:dutyList){
				User member = duty.getUser();
				rMap = new HashMap<String, Object>();
				rMap.put("id", duty.getId());
				rMap.put("isLeader", duty.equals(leader) ? 1 : 0);
				rMap.put("userId", "");
				rMap.put("userName", "");
				rMap.put("postName", "");
				if(member!=null){
					rMap.put("userId", member.getId());
					rMap.put("userName", member.getName());
				}
				Power power = duty.getPower();
				if(power!=null){
					rMap.put("postName", power.getPost()!=null ? power.getPost().getName() : "");
				}
				dataRows.add(rMap);
			}
			data.put("data", dataRows);
			data.put("recordsTotal", searchResult.getTotalCount());
			data.put("recordsFiltered", searchResult.getTotalCount());
		}
		data.put("draw", draw);
		return data;
	}

}
